package com.xdk.templatescala.controller;/**
 * @program: template-scala-dev
 * @name CommonController
 * @description: 公共
 */

import com.xdk.templatescala.common.response.Result;
import com.xdk.templatescala.config.ScaleConfig;
import com.xdk.templatescala.utils.file.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 *@author:
 *@program: template-scala-dev
 *@name CommonController
 *@description: 公共
 */
@Slf4j
@Api(value = "文件上传接口信息", tags = {"WEB-文件上传接口信息"})
@RestController
@RequestMapping("/common")
public class CommonController {

    @ApiOperation("通用文件上传")
    @PostMapping("/upload")
    public Result<String> uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = ScaleConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            //返回添加附件记录id
            return Result.SUCCESS(fileName);
        } catch (Exception e) {
            log.error("文件上传失败",e);
        }
       return Result.FAIL("文件上传失败");
    }
}
